package com.aramis.mydytt.ui.base

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import java.io.Serializable

/**
 * Description: SimpleRecyclerAdapter
 * @author lizhidan
 * Created: 2021/5/24
 */
abstract class SimpleRecyclerAdapter<M : Serializable, T : BaseViewHolder<M>> : RecyclerView.Adapter<T>() {
    val dataList = mutableListOf<M>()
    var onItemClickListener: ((M) -> Unit)? = null

    fun setData(list: List<M>?) {
        dataList.clear()
        list?.let { dataList.addAll(it) }
        notifyDataSetChanged()
    }

    override fun onBindViewHolder(holder: T, position: Int) {
        holder.bind(dataList[position], position)
    }

    override fun getItemCount(): Int = dataList.size

    protected fun createView(parent: ViewGroup, id: Int): View {
        return LayoutInflater.from(parent.context).inflate(id, parent, false)
    }
}